package cn.com.aiouyi.dao.impl;

import cn.com.aiouyi.dao.ArticleDao;
import cn.com.aiouyi.entity.Article;
import cn.com.aiouyi.entity.ArticleClass;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.NoResultException;

/**
 * Dao - 文章
 * @author tommy
 *
 */
@Repository("articleDaoImpl")
public class ArticleDaoImpl extends BaseDaoImpl<Article, Long> implements ArticleDao{

    @Override
    public Article findExist(ArticleClass articleClass, String title, String author) {
        if (StringUtils.isEmpty(title)) {
            return null;
        }
        if (StringUtils.isEmpty(author)) {
            return null;
        }
        try {
            String jpql = "select article from Article article where article.articleClass = : articleClass " +
                    "and article.title = :title and article.author = :author";
            return entityManager.createQuery(jpql, Article.class)
                    .setParameter("articleClass", articleClass).
                            setParameter("title", title)
                    .setParameter("author", author).getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }
}
